package com.yunji.order.dao.impl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.order.dao.IRefundDetailDao;
import com.yunji.order.domain.OrderDetailDO;
import com.yunji.order.domain.RefundDetailDO;
import com.yunji.order.dto.OrderDetailRefundDTO;
import com.yunji.order.dto.RefundDetailParam;
import com.yunji.order.mapper.IRefundDetailMapper;
import com.yunji.order.vo.RefundDetailVO;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2025/3/26 14:04
 * @description : TODO
 **/
@Repository
public class RefundDetailDaoImpl extends BaseDaoImpl<IRefundDetailMapper, RefundDetailDO> implements IRefundDetailDao {

    @Override
    public List<OrderDetailRefundDTO> getDetailNumsByOid(List<Long> orderIds, List<Integer> statusList) {
        if (orderIds == null || orderIds.isEmpty())
            return Lists.newArrayList();
        return baseMapper.getDetailNumsByOid(orderIds, statusList);
    }

    @Override
    public List<OrderDetailRefundDTO> getStatusNumsByOid(List<Long> orderIds) {
        if (orderIds == null || orderIds.isEmpty())
            return Lists.newArrayList();
        return baseMapper.getStatusNumsByOid(orderIds);
    }

    @Override
    public List<RefundDetailDO> getDetailsByRids(List<Long> refundIds) {
        return lambdaQuery().in(RefundDetailDO::getRefundId, refundIds).list();
    }

    @Override
    public List<RefundDetailVO> getDetailList(RefundDetailParam param) {
        MPJLambdaWrapper<RefundDetailDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(RefundDetailDO.class).leftJoin(OrderDetailDO.class, OrderDetailDO::getOrderDetailId, RefundDetailDO::getOrderDetailId)
                .select(OrderDetailDO::getSku).select(OrderDetailDO::getProductName)
                .select(OrderDetailDO::getProductTypeName).select(OrderDetailDO::getAlbumPics)
                .select(OrderDetailDO::getPrice).select(OrderDetailDO::getCostPrice)
                .select(OrderDetailDO::getSiteSalePrice).select(OrderDetailDO::getProductNums)
                .eq(param.getRefundId() != null, RefundDetailDO::getRefundId, param.getRefundId())
                .in(param.getRefundIds() != null, RefundDetailDO::getRefundId, param.getRefundIds())
                .orderByDesc(RefundDetailDO::getCreateName);
        return selectJoinList(RefundDetailVO.class, wrapper);
    }
}